﻿using System;
using System.Windows.Media;

namespace Songhay
{
    /// <summary>
    /// Extensions of <see cref="System.Windows.Media.Color"/>.
    /// </summary>
    /// <remarks>
    /// Silverlight does not have the <c>BrushConverter</c> class.
    /// </remarks>
    public static class ColorUtility
    {
        /// <summary>
        /// Color utility: get from hex value.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="hexValue">The hex value.</param>
        /// <returns></returns>
        public static Color GetFromHexValue(string hexValue)
        {
            return ColorUtility.GetFromHexValue(hexValue, defaultHexValue: "#000");
        }

        /// <summary>
        /// Color utility: get from hex value.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="hexValue">The hex value.</param>
        /// <param name="defaultHexValue">The default hex value.</param>
        public static Color GetFromHexValue(string hexValue, string defaultHexValue)
        {
            var getColor = new Func<string, Color>(
                s =>
                {
                    if(string.IsNullOrEmpty(hexValue)) return Color.FromArgb(255, 0, 0, 0);

                    byte a;
                    byte r;
                    byte g;
                    byte b;

                    s = s.TrimStart('#');
                    if(s.Length == 3)
                    {
                        a = 255;
                        r = Convert.ToByte(string.Format("{0}{0}", s.Substring(0, 1)), 16);
                        g = Convert.ToByte(string.Format("{0}{0}", s.Substring(1, 1)), 16);
                        b = Convert.ToByte(string.Format("{0}{0}", s.Substring(2, 1)), 16);
                        return Color.FromArgb(a, r, g, b);
                    }
                    else if(s.Length == 6)
                    {
                        a = 255;
                        r = Convert.ToByte(s.Substring(0, 2), 16);
                        g = Convert.ToByte(s.Substring(2, 2), 16);
                        b = Convert.ToByte(s.Substring(4, 2), 16);
                        return Color.FromArgb(a, r, g, b);
                    }
                    else if(s.Length == 8)
                    {
                        a = Convert.ToByte(s.Substring(0, 2), 16);
                        r = Convert.ToByte(s.Substring(2, 2), 16);
                        g = Convert.ToByte(s.Substring(4, 2), 16);
                        b = Convert.ToByte(s.Substring(6, 2), 16);
                        return Color.FromArgb(a, r, g, b);
                    }
                    else
                    {
                        return Color.FromArgb(255, 0, 0, 0);
                    }
                });

            if(string.IsNullOrEmpty(hexValue) && string.IsNullOrEmpty(defaultHexValue))
            {
                return Color.FromArgb(255, 0, 0, 0);
            }
            else if(string.IsNullOrEmpty(hexValue) && !string.IsNullOrEmpty(defaultHexValue))
            {
                return getColor(defaultHexValue);
            }
            else
            {
                return getColor(hexValue);
            }
        }
    }
}
